﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Hotel.Classes.Helper;
using Hotel.Models.Entity;

namespace Hotel.Models.DataAccess
{
    public class DAPay
    {
        public static List<Pay> GetListPay()
        {
            DataTable dt;
            List<Pay> list = new List<Pay>();
            using (DBManager dbManager = new DBManager("stp_Get_Pay"))
            {
                dt = dbManager.GetDatatable();
            }
            foreach (DataRow dr in dt.Rows)
            {
                Pay pay = new Pay();
                pay.PkPayId = DataHelper.GetInt(dr, "PK_PayID");

                Staff staff = new Staff();
                staff.PkStaffId = DataHelper.GetInt(dr, "FK_StaffID");
                pay.Staff = staff;

                OptionPay optionPay = new OptionPay();
                optionPay.PkOptionPayId = DataHelper.GetInt(dr, "FK_OptionPayID");
                pay.OptionPay = optionPay;

                pay.Description = DataHelper.Get(dr, "Description");
                pay.VndPay = DataHelper.GetDouble(dr, "VNDPay");
                pay.UsdPay = DataHelper.GetDouble(dr, "USDPay");
                pay.Recipient = DataHelper.Get(dr, "Recipient");
                pay.DatePay = DataHelper.GetDate(dr, "DatePay");
                list.Add(pay);
            }
            return list;
        }

        public static bool SavePay(Pay pay)
        {
            Staff staff = pay.Staff;
            OptionPay option = pay.OptionPay;
            using (DBManager dbManager = new DBManager("stp_Create_Pay"))
            {
                try
                {
                    dbManager.Add("p_FK_StaffID", staff.PkStaffId);
                    dbManager.Add("p_FK_OptionPayID", option.PkOptionPayId);
                    dbManager.Add("p_Description", pay.Description);
                    dbManager.Add("p_VNDPay", pay.VndPay);
                    dbManager.Add("p_USDPay", pay.UsdPay);
                    dbManager.Add("p_Recipient", pay.Recipient);
                    dbManager.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }
    }
}